import pickle

class Data:
	def __init__(self, user="", password=""):
		self.user = user
		self.password = password
		self.server = "talk.google.com"
		self.port = 5223
		self.domain = "gmail.com"
		self.allowedJIDs = []
	def AddAllowedJID(self, jid):
		isPresent = self.allowedJIDs.count(jid) > 0
		if not isPresent: self.allowedJIDs.append(jid)
	def IsJIDAllowed(self, jid):
		return self.allowedJIDs.count(jid) > 0
	def AcceptUserInput(self):
		self.user = raw_input("Enter GTalk Username:").strip()
		self.password = raw_input("Enter Gtalk password:").strip()
		self.server = raw_input("Enter Gtalk Server [def=\"talk.google.com\"]:").strip()
		self.pot = raw_input("Enter port[def=5223]:").strip()
		self.domain = raw_input("Enter Domain [def=\"gmail.com\"]:").strip()
		print "Allowed Allowed Gtalk Id's one on each line (end with Empty)"
		notDone = True
		while (notDone):
			try:
					str = raw_input().strip()
					if str != "":
						self.AddAllowedJID(str)
					else:
						notDone = False
			except EOFError:
					notDone = False

		if (self.server == ""): self.server = "talk.google.com"
		if (self.domain == ""): self.domain = "gmail.com"
	def DumpInformation(self):
		print "\
	Username     : %s  \n\
	Password     : %s  \n\
	Server       : %s  \n\
	Domain       : %s  \n\
	AllowedJID   : %s  \n\
			" % (self.user, self.password, self.server, self.domain, self.allowedJIDs,)
					
	
class DataPickler:
		def Serialize(self, data):
			data.DumpInformation()
			f = open(self.PickFile(), "w")
			pickle.dump(data, f)
		def Deserialize(self):
			try:
				f = open(self.PickFile(), "r")
				data = pickle.load(f)
			except IOError:
				#file doesn't exist create it.
				f = open(self.PickFile(), "w")
				data = Data()
				data.AcceptUserInput()
				pickle.dump(data, f)
			#if (data == None)
			print "Read:\n"
			data.DumpInformation()
			return data
		def PickFile(self):
			return "IMShell.dat"
